{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import csv\n",
    "from sklearn.metrics import accuracy_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_train_path = \"./data/x_train_process.csv\"\n",
    "y_train_path = \"./data/y_train_process.csv\"\n",
    "x_test_path = \"./data/x_test_process.csv\"\n",
    "y_test_path = \"./data/y_test_process.csv\"\n",
    "\n",
    "def load_data(file_name):\n",
    "    bank_data = []\n",
    "    with open(file_name) as csvfile:\n",
    "        csv_reader = csv.reader(csvfile)\n",
    "        data_header = next(csv_reader)  \n",
    "        for row in csv_reader:  \n",
    "            bank_data.append(row)\n",
    "    return bank_data\n",
    "\n",
    "x_train = np.array(load_data(x_train_path)).astype(float)\n",
    "y_train = np.array(load_data(y_train_path)).astype(float)\n",
    "x_test = np.array(load_data(x_test_path)).astype(float)\n",
    "y_test = np.array(load_data(y_test_path)).astype(float)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 一、 支持向量机"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练集精确度:  1.0\n",
      "测试集精确度:  0.975\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/ubuntu/lib/python3.5/site-packages/sklearn/utils/validation.py:724: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
      "  y = column_or_1d(y, warn=True)\n"
     ]
    }
   ],
   "source": [
    "from sklearn.svm import SVC, NuSVC, LinearSVC\n",
    "model = SVC(kernel='rbf', degree=2, gamma=1.7)\n",
    "# 拟合训练数据集\n",
    "model.fit(x_train, y_train)\n",
    "# 预测训练集\n",
    "train_predict_y = model.predict(x_train)\n",
    "print(\"训练集精确度: \", accuracy_score(y_train, train_predict_y))\n",
    "# 预测测试集\n",
    "test_y_predict = model.predict(x_test)\n",
    "print(\"测试集精确度: \", accuracy_score(y_test, test_y_predict))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 二、 逻辑回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练集精确度:  0.9775\n",
      "测试集精确度:  0.975\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/ubuntu/lib/python3.5/site-packages/sklearn/linear_model/logistic.py:432: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.\n",
      "  FutureWarning)\n",
      "/home/ubuntu/lib/python3.5/site-packages/sklearn/utils/validation.py:724: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
      "  y = column_or_1d(y, warn=True)\n"
     ]
    }
   ],
   "source": [
    "from sklearn.linear_model.logistic import LogisticRegression\n",
    "LR = LogisticRegression()\n",
    "LR.fit(x_train, y_train)\n",
    "train_predict_y = LR.predict(x_train)\n",
    "print(\"训练集精确度: \", accuracy_score(y_train, train_predict_y))\n",
    "test_y_predict = LR.predict(x_test)\n",
    "print(\"测试集精确度: \", accuracy_score(y_test, test_y_predict))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 三、 神经网络"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练集精确度:  0.9825\n",
      "测试集精确度:  0.966666666667\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/ubuntu/lib/python3.5/site-packages/sklearn/neural_network/multilayer_perceptron.py:921: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
      "  y = column_or_1d(y, warn=True)\n"
     ]
    }
   ],
   "source": [
    "from sklearn.neural_network import MLPClassifier\n",
    "mlp = MLPClassifier(solver='lbfgs', alpha=1e-5,hidden_layer_sizes=(5, 5), random_state=1)\n",
    "mlp.fit(x_train, y_train)\n",
    "print(\"训练集精确度: \", mlp.score(x_train, y_train))\n",
    "print(\"测试集精确度: \", mlp.score(x_test, y_test))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 四、 决策树"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练集精确度:  1.0\n",
      "测试集精确度:  0.958333333333\n"
     ]
    }
   ],
   "source": [
    "from sklearn.tree import DecisionTreeClassifier\n",
    "tree = DecisionTreeClassifier(random_state=0)\n",
    "tree.fit(x_train, y_train)\n",
    "print(\"训练集精确度: \", tree.score(x_train, y_train))\n",
    "print(\"测试集精确度: \", tree.score(x_test, y_test))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
